// date.js
import moment from 'moment';

export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length);
}

export function str2Date(dateStr, separator) {
  if (!separator) {
    separator = "-";
  }
  let dateArr = dateStr.split(separator);
  let year = parseInt(dateArr[0]);
  let month;
  //处理月份为04这样的情况
  if (dateArr[1].indexOf("0") == 0) {
    month = parseInt(dateArr[1].substring(1));
  } else {
    month = parseInt(dateArr[1]);
  }
  let day = parseInt(dateArr[2]);
  let date = new Date(year, month - 1, day);
  return date;
}

/**
 * 快捷格式化日期
 */
export function momentFormatTime(value, format) { 
  if(!value){
    return moment().format( format || "YYYY-MM-DD")
  }
  return moment(value).format(format || "YYYY-MM-DD")
}



export {
  dateRange
}
/**
 * Created by Administrator on 2015/12/11.
 */
const dateRange = {
  currentDate: new Date(),
  millisecond: 1000 * 60 * 60 * 24, //一天的毫秒数
  /**
   * 获得今天的起止日期
   */
  getToday: function () {
    var startStop = new Array();
    var date = new Date();
    startStop.push(dateRange.formatterDate(date));
    startStop.push(dateRange.formatterDate(date));
    return startStop;
  },
  /**
   * 获得昨天的起止日期
   */
  getYesterday: function () {
    var startStop = new Array();
    const date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24);
    startStop.push(dateRange.formatterDate(date));
    startStop.push(dateRange.formatterDate(date));
    return startStop;
  },
  /**
   * 获得前天的起止日期
   */
  getBeforeYesterday: function () {
    var startStop = new Array();
    const date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24 * 2);
    startStop.push(dateRange.formatterDate(date));
    startStop.push(dateRange.formatterDate(date));
    return startStop;
  },
  /**
   * 获得本周的起止日期
   */
  getThisWeek: function () {
    //起止日期数组
    var startStop = new Array();
    //返回date是一周中的某一天
    var week = dateRange.currentDate.getDay();
    //减去的天数
    var minusDay = week != 0 ? week - 1 : 6;
    //本周 周一
    var monday = new Date(dateRange.currentDate.getTime() - (minusDay * dateRange.millisecond));
    //本周 周日
    var sunday = new Date(monday.getTime() + (6 * dateRange.millisecond));
    //添加本周时间
    startStop.push(dateRange.formatterDate(monday)); //本周起始时间
    //添加本周最后一天时间
    startStop.push(dateRange.formatterDate(sunday)); //本周终止时间
    //返回
    return startStop;
  },
  /**
   * 获得上一周的起止日期
   */
  getLastWeek: function () {
    //起止日期数组
    var startStop = new Array();
    //返回date是一周中的某一天
    var week = dateRange.currentDate.getDay();
    //减去的天数
    var minusDay = week != 0 ? week - 1 : 6;
    //获得当前周的第一天
    var currentWeekDayOne = new Date(dateRange.currentDate.getTime() - (dateRange.millisecond * minusDay));
    //上周最后一天即本周开始的前一天
    var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - dateRange.millisecond);
    //上周的第一天
    var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (dateRange.millisecond * 6));

    startStop.push(dateRange.formatterDate(priorWeekFirstDay));
    startStop.push(dateRange.formatterDate(priorWeekLastDay));
    //返回
    return startStop;
  },
  /**
   * 获得这个月的起止日期
   */
  getThisMonth: function () {
    var startStop = new Array();
    //获得当前月份0-11
    var currentMonth = dateRange.currentDate.getMonth();
    //获得当前年份4位年
    var currentYear = dateRange.currentDate.getFullYear();
    //求出本月第一天
    var firstDay = new Date(currentYear, currentMonth, 1);
    //当为12月的时候年份需要加1
    //月份需要更新为0 也就是下一年的第一个月
    //否则只是月份增加,以便求的下一月的第一天
    if (currentMonth == 11) {
      currentYear++;
      currentMonth = 0;
    } else {
      currentMonth++;
    }
    //下月的第一天
    var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
    //求出上月的最后一天
    var lastDay = new Date(nextMonthDayOne.getTime() - dateRange.millisecond);

    startStop.push(dateRange.formatterDate(firstDay));
    startStop.push(dateRange.formatterDate(lastDay));
    //返回
    return startStop;
  },
  /**
   * 获得上个月的起止日期
   */
  getLastMonth: function () {
    var startStop = new Array();
    //获得当前月份0-11
    var currentMonth = dateRange.currentDate.getMonth();
    //获得当前年份4位年
    var currentYear = dateRange.currentDate.getFullYear();
    var currentDay = new Date(currentYear, currentMonth, 1);
    //上个月的第一天
    //年份为0代表,是本年的第一月,所以不能减
    if (currentMonth == 0) {
      currentMonth = 11; //月份为上年的最后月份
      currentYear--; //年份减1
    } else {
      currentMonth--;
    }
    var firstDay = new Date(currentYear, currentMonth, 1);
    //求出上月的最后一天
    var lastDay = new Date(currentDay.getTime() - dateRange.millisecond);

    startStop.push(dateRange.formatterDate(firstDay));
    startStop.push(dateRange.formatterDate(lastDay));
    //返回
    return startStop;
  },
  /**
   * 获得今年的起止日期
   */
  getThisYear: function () {
    var startStop = new Array();
    //求出今年第一天
    var firstDay = new Date();
    firstDay.setDate(1);
    firstDay.setMonth(0);
    var lastDay = new Date();
    lastDay.setFullYear(lastDay.getFullYear() + 1); // 设置到明年
    lastDay.setMonth(0); // 明年的0月，也就是对应到1月，是存在的哦，不是不存在的0
    lastDay.setDate(0); // 明年的0日
    startStop.push(dateRange.formatterDate(firstDay));
    startStop.push(dateRange.formatterDate(lastDay));
    //返回
    return startStop;
  },
  /**
   * 获得去年的起止日期
   */
  getLastYear: function () {
    var startStop = new Array();
    //求出今年第一天
    var firstDay = new Date();
    firstDay.setDate(1);
    firstDay.setMonth(0);
    var lastDay = new Date();
    lastDay.setFullYear(lastDay.getFullYear()); // 设置到今年
    lastDay.setMonth(0); // 明年的0月，也就是对应到1月，是存在的哦，不是不存在的0
    lastDay.setDate(0); // 明年的0日
    startStop.push(dateRange.formatterDate(firstDay));
    startStop.push(dateRange.formatterDate(lastDay));
    //返回
    return startStop;
  },
  /**
   * 格式化日期（不含时间）
   */
  formatterDate: function (date) {
    var year = date.getFullYear();
    var month = ((date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1));
    var day = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
    var datetime = year + "-" + month + "-" + day;
    return datetime;
  }
}

export function shortcutsResult() {
  return [{
    text: '今天',
    onClick(picker) {
      picker.$emit('pick', dateRange.getToday());
    }
  },
  {
    text: '昨天',
    onClick(picker) {
      picker.$emit('pick', dateRange.getYesterday());
    }
  },
  {
    text: '前天',
    onClick(picker) {
      picker.$emit('pick', dateRange.getBeforeYesterday());
    }
  },
  {
    text: '本周',
    onClick(picker) {
      picker.$emit('pick', dateRange.getThisWeek());
    }
  },
  {
    text: '上周',
    onClick(picker) {
      picker.$emit('pick', dateRange.getLastWeek());
    }
  },
  {
    text: '本月',
    onClick(picker) {
      picker.$emit('pick', dateRange.getThisMonth());
    }
  },
  {
    text: '上月',
    onClick(picker) {
      picker.$emit('pick', dateRange.getLastMonth());
    }
  },
  {
    text: '今年',
    onClick(picker) {
      picker.$emit('pick', dateRange.getThisYear());
    }
  }
  ];
}